/*
 * Email Notify
 * Copyright: (C) 2008 by Antonio Nastasi
 * Email: sifcenter (at) gmail.it
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "account.h"

#include <KConfig>
#include <KDebug>


Account::Account( KConfigGroup& config ) :
    login_(""),
    password_(""),
    server_(""),
    currentEmails_(-1) // Not synched
{
  generalGroup_ = config;
}



Account::~Account()
{
  kDebug() << "DESTROYED";
}



const QString& Account::getLogin()             const { return login_; }

const QString&  Account::getPassword()          const { return password_; }

const QString&  Account::getServer()            const { return server_; }

int     Account::getPort()              const { return port_; }

const QString&  Account::getProtocol()          const { return protocol_; }

bool    Account::isSSL()                const { return ssl_; }

int     Account::getInterval()          const { return interval_; }

const QString&  Account::getSoundFile()         const { return soundFile_; }

/*bool Account::getAnimate()            const { return animate_; }*/

int     Account::getPosition()          const { return position_; }

bool    Account::getShortCaption()      const { return shortCaption_; }

/*bool Account::getBalloon()            const { return balloon_; }*/

int     Account::getCurrentEmails()     const { return currentEmails_; }

const QFont&   Account::getFont()              const { return font_; }

const QColor&  Account::getFontColor()         const { return fontColor_; }

const QString&  Account::getClientEmail()       const { return clientEmail_; }



void Account::setCurrentEmails( int currentEmails )
{
  kDebug() << "Emails count changed!";

  // If this isn't the first reading of email account then
  // emit the new email received signal
  if( currentEmails_ != -1 && currentEmails != currentEmails_ )
  {
    emit newEmailReceived();
  }

  // Change the emails count
  currentEmails_ = currentEmails;
  emit currentEmailsChanged();
}



void Account::setShortCaption( bool mode )
{
  shortCaption_ = mode;
}



void Account::slotReadConfig()
{
  // Reparse configuration to reload the current account settings
  generalGroup_.config()->reparseConfiguration ();

  // Reset currentEmails if server or login changed!
  if( ( server_ !=  getServer() ) ||
      ( login_  !=  getLogin()  ) )
  {
    currentEmails_ = -1;
  }

  login_    =   generalGroup_.readEntry( "Login",              "" );
  password_ =   QByteArray::fromBase64( generalGroup_.readEntry( "Password", QString("") ).toUtf8() );
  server_   =   generalGroup_.readEntry( "Server",             "" );
  port_=        generalGroup_.readEntry( "Port",               0 );
  protocol_ =   generalGroup_.readEntry( "Protocol",           "" );
  ssl_ =        generalGroup_.readEntry( "SSL",                false );
  interval_ =   generalGroup_.readEntry( "Interval",           100 );
  soundFile_ =  generalGroup_.readEntry( "Sound",              "(Nothing)" );
  //animate_ =  generalGroup_.readEntry( "Animate",            QVariant().toBool() );
  position_ =   generalGroup_.readEntry( "Position",           0 );
  shortCaption_=generalGroup_.readEntry( "ShortCaption",       false );
  //balloon_ =  generalGroup_.readEntry( "Balloon",            QVariant().toBool() );
  font_ =       generalGroup_.readEntry( "Font",               QFont() );
  fontColor_ =  generalGroup_.readEntry( "FontColor",          QColor( "white" ) );
  clientEmail_ =generalGroup_.readEntry( "EmailClient",        "" );
  
  emit accountUpdated();
}
